<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-10 19:42:35
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-14 20:35:21
 */

namespace App\Console\Commands\Wblog;

use AipImageCensor;
use App\Models\Comment;
use Illuminate\Console\Command;

class AuditComment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wblog:auditComment';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Audit Comment';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $baiduConfig = [
            config('services.baidu.appid'),
            config('services.baidu.appkey'),
            config('services.baidu.secret'),
        ];

        if(count(array_filter($baiduConfig)) === 3){
            $comments = Comment::select('id', 'content', 'is_audited')->withTrashed()->get();

            $baiduClient = new AipImageCensor(config('services.baidu.appid'), 
                config('services.baidu.appkey'), config('services.baidu.secret'));

            $bar = $this->output->createProgressBar($comments->count());
            $bar->start();
            $count = 0;

            foreach($comments as $comment){
                $bar->advance();

                if($comment->is_audited === 0){
                    continue;
                }

                $count++;

                $content = $comment->getOriginal('content');
                $result = $baiduClient->antiSpam($content);

                if(!isset($result['result']['spam'])){
                    $this->error('error: ' . json_encode($result));
                }else{
                    $comment->timestamps = false;
                    $comment->is_audited = $result['result']['spam'] === 0 ? 1 : 0;
                    $comment->save();

                    $message = 'id:' . $comment->id . 'is_audited:' . $comment->is_audited . 'content:' . $content;

                    if($comment->is_audited === 1){
                        $this->info($message);
                    }else{
                        $this->error($message);
                    }
                }

                if($count === 5){
                    sleep(1);
                    $count = 0;
                }
            }

            $bar->finish();
        }else{
            $this->error('请先配置百度的 key ');
        }
    }
}
